package com.example.controller;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.util.StrUtil;
import com.example.entity.dto.UserAddEvaluationRecordDTO;
import com.example.entity.dto.UserGetParticipationRecordsDTO;
import com.example.service.AuditRecordService;
import com.example.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/auditRecord")
@Tag(name = "审核评价", description = "审核评价获取评价")
public class AuditRecordController {

	@Autowired
	private AuditRecordService auditRecordService;

	@Operation(summary = "自定义条件获取用户的参与记录")
	@PostMapping("getParticipationRecords")
	public SaResult getParticipationRecords(@RequestBody UserGetParticipationRecordsDTO userGetParticipationRecordsDTO
			, @RequestHeader("token") String token) {
		String userId = JwtUtil.parseTokenId(token);
		userGetParticipationRecordsDTO.setUserId(Long.valueOf(userId));
		// 使用 Optional 处理默认值 如果为空或者null就设置默认值
		Integer statusId = Optional.ofNullable(userGetParticipationRecordsDTO.getStatusId()).orElse(0);
		if (StrUtil.isBlank(userGetParticipationRecordsDTO.getDate())) {
			userGetParticipationRecordsDTO.setDate("all");
		}

		// 更新 DTO 中的值（可选）
		userGetParticipationRecordsDTO.setStatusId(statusId);
		return SaResult.data(auditRecordService.getParticipationRecords(userGetParticipationRecordsDTO));
	}


	@Operation(summary = "新增用户提交的评价")
	@PostMapping("addEvaluationRecordsSubmit")
	public SaResult addEvaluationRecordsSubmittedByUser(@RequestBody UserAddEvaluationRecordDTO userAddEvaluationRecordDTO, @RequestHeader("token") String token) {
		auditRecordService.addEvaluationRecordsSubmittedByUser(userAddEvaluationRecordDTO, token);
		return SaResult.ok("提交成功");
	}


}